import {createStore} from 'vuex';
import createPersistedState from 'vuex-persistedstate';

//创建一个新的store实例
const store = createStore({
    state() {
        return {
            userInfo: {
                id: '',
                username: '',
                type: 0,
                token: ''
            },
            isLogin: false,
            idAddRoute: false,
            newMessageCount: 0,
            chatList: [],//聊天列表
            currentChatId: 0,//当前聊天用户id
            connectStatus: false,//socket连接状态
            currentUserId: 0,//当前查看的用户id
        }
    },
    getters: {
        checkLogin(state) {
            return state.isLogin;
        },
        getUserInfo(state) {
            return state.userInfo;
        },
        checkAddRoute(state) {
            return state.idAddRoute;
        },
        getNewMessageCount(state) {
            return state.newMessageCount;
        },
        getChatList(state) {
            return state.chatList;
        },
        getCurrentChatId(state) {
            return state.currentChatId;
        },
        getConnectStatus(state) {
            return state.connectStatus;
        },
        getCurrentUserId(state) {
            return state.currentUserId;
        }
    },
    mutations: {
        changeLogin(state, login) {
            state.isLogin = login;
        },
        changeUserInfo(state, info) {
            state.userInfo = Object.assign(state.userInfo, info);
        },
        changeAddRoute(state, isAdd) {
            state.idAddRoute = isAdd;
        },
        changeNewMessageCount(state, count) {
            state.newMessageCount = count;
        },
        changeChatList(state, list) {
            state.chatList = list;
        },
        changeCurrentChatId(state, id) {
            state.currentChatId = id;
        },
        changeConnectStatus(state, status) {
            state.connectStatus = status;
        },
        changeCurrentUserId(state, id) {
            state.currentUserId = id;
        }
    },
    actions: {
        handleChangeLogin(context, login) {
            context.commit('changeLogin', login);
        },
        handleChangeUserInfo(context, info) {
            context.commit('changeUserInfo', info);
        },
        handleChangeIsAddRouter(context, isAdd) {
            context.commit('changeAddRoute', isAdd);
        },
        handleChangeNewMessageCount(context, count) {
            context.commit('changeNewMessageCount', count);
        },
        handleChangeChatList(context, list) {
            context.commit('changeChatList', list);
        },
        handleCurrentChatId(context, id) {
            context.commit('changeCurrentChatId', id);
        },
        handleChangeConnectStatus(context, status) {
            context.commit('changeConnectStatus', status);
        },
        handleCurrentUserId(context, id) {
            context.commit('changeCurrentUserId', id);
        }
    },
    plugins: [createPersistedState({
        key: 'vuexState',
        storage: window.localStorage,
        reducer(val) {
            return {
                userInfo: val.userInfo,
                isLogin: val.isLogin
            }
        }
    })]
});

export default store;